<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : credits.class.php
 * ....积分设置操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Wed Jun 03 10:09:18 CST 2009
 */

class CreditsClass extends FrameWork {
	/**
	 * 记录用户操作的积分日志
	 * @author workflow Develop Team     
	 * @param  $log_type 操作 
	 * @param  $member_id 会员ID
	 * @param  $if_repeat 是否可以重复记录的信息,true 可以重复记录，false不可以重复记录，默认为true
	 * @return bool
	 */
	function saveCreditsLog($log_type,$member_id,$if_repeat = true){
		//初始化变量
		$this->_getConfiginfo();
		//加载语言包
		$this->getlang("credits");
		//积分项目
		require_once('settings.class.php');
		$obj_settings = new SettingsClass();
		$extcredits_exp = unserialize($obj_settings->getSettings('extcredits_exp'));
		$extcredits_points = unserialize($obj_settings->getSettings('extcredits_points'));
		unset($obj_settings);
		//记录原因文字
		switch ($log_type){
			case 'regist':
				$log_content = $this->_lang['langCreditsRegist'];
				break;
			case 'succ_product_put':
				$log_content = $this->_lang['langCreditsSuccProductPut'];
				break;
			case 'succ_buy':
				$log_content = $this->_lang['langCreditsSuccBuy'];
				break;
			case 'open_shop':
				$log_content = $this->_lang['langCreditsOpenShop'];
				break;
			case 'member_certify':
				$log_content = $this->_lang['langCreditsMemberCertify'];
				break;
			case 'shop_entity':
				$log_content = $this->_lang['langCreditsShopEntity'];
				break;
			case 'good_score':
				$log_content = $this->_lang['langCreditsGoodScore'];
				break;
			case 'normal_score':
				$log_content = $this->_lang['langCreditsNormalScore'];
				break;
			case 'bad_score':
				$log_content = $this->_lang['langCreditsBadScore'];
				break;
//			case 'succ_report':
//				$log_content = '会员的投诉举报操作成功';
//				break;
			default:
				echo $this->_lang['errCreditsHaveNone'];
				exit;
		}
		if ($member_id == ''){
			return false;
		}
		$save_sign = true;
		if ($if_repeat == false){
			//检测是否有相关信息存在，如果没有，入库
			$condition['member_id'] = $member_id;
			$condition['type'] = $log_type;
			$log_array = self::getCreditsLogList($condition,$page);
			if (!empty($log_array)){
				$save_sign = false;
			}
		}
		if ($save_sign == false){
			return true;
		}
		//新增日志
		$value_array = array();
		$value_array['cl_member_id'] = $member_id;
		$value_array['cl_time'] = time();
		$value_array['cl_type'] = $log_type;
		$value_array['cl_content'] = $log_content;
		$value_array['cl_exp'] = $extcredits_exp[$log_type]?$extcredits_exp[$log_type]:0;
		$value_array['cl_points'] = $extcredits_points[$log_type]?$extcredits_points[$log_type]:0;
		self::addCreditsLog($value_array);
		unset($value_array);
		//更新member内容
		require_once('member.class.php');
		$obj_member = new MemberClass();
		$value_array = array();
		$value_array['extcredits_exp'] = $extcredits_exp[$log_type];
		$value_array['extcredits_points'] = $extcredits_points[$log_type];
		$obj_member->modifyMember($value_array,$member_id,'credits');
		unset($obj_member,$value_array);
		return true;
	}
	
	/**
	 * 新增积分日志
	 * @author workflow Develop Team     
	 * @param  $input_param 参数
	 * @return bool
	 */
	function addCreditsLog($input_param){
		$arr = array();
		if (is_array($input_param)){
			foreach ($input_param as $k => $v){
				$arr[$k] = $v;
			}
		}
		$result = $GLOBALS['db']->InsertRow($arr,"credits_log","cl_id");
		return $result;
	}
	
	/**
	 * 取日志列表
	 * @author workflow Develop Team     
	 * @param  $condition 索引条件
	 * @param  $obj_page 分页
	 * @param  $genre 取值类别
	 * @return array
	 */
	function getCreditsLogList($condition,&$obj_page,$genre='simple'){
		$condition_str = self::_conditionSearch($condition);
		if ($genre == 'simple'){
			$log_array = $GLOBALS['db']->GetList($condition_str,$obj_page,'credits_log');
		}
		if ($genre == 'more'){
			$log_array = $GLOBALS['db']->GetJoinList($obj_page,array('credits_log','member'),"left join",array("credits_log.cl_member_id=member.member_id"),array('*','login_name'),$condition_str);
		}
		return $log_array;
	}
	
	/**
	 * 构造检索条件
	 * @author workflow Develop Team     
	 * @param  $condition 检索条件
	 * @return string
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		
		if ($condition['member_id'] != ''){
			$condition_str .= " and credits_log.cl_member_id = '" . $condition['member_id'] . "'";
		}
		if ($condition['type'] != ''){
			$condition_str .= " and credits_log.cl_type = '" . $condition['type'] . "'";
		}
		if ($condition['member_name'] != ''){
			$condition_str .= " and member.login_name = '" . $condition['member_name'] . "'";
		}
		if ($condition['order_by'] != ''){
			$condition_str .= " order by ".$condition['order_by'];
		}
		
		return $condition_str;
	}
}
?>